import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;

public class StudyGuavaCache {
    public static void main(String[] args) throws InterruptedException {
        Cache<String,Object> cache = CacheBuilder.newBuilder()
                .maximumSize(2)
//                .initialCapacity()
//                .maximumWeight()
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .weakValues()
                .build();
        Object value = new Object();
        cache.put("key1",value);


        value = new Object();//原对象不再有强引用
        System.gc();
        System.out.println(cache.getIfPresent("key1"));

        cache.invalidateAll();//批量清除list中全部key对应的记录
    }
}